//
//  HomeSheetPopView.m
//  location
//
//  Created by Dongxiang Xu on 2025/5/8.
//

#import "HomeSheetPopView.h"
#import "HomeViewController.h"

@interface HomeSheetPopView ()



@end

@implementation HomeSheetPopView

- (instancetype)initWithFrame:(CGRect)frame
                       StartY:(CGFloat)startY
                     TGroupId:(NSString *)groupId
                     IsManage:(BOOL)manage
{
    self = [super initWithFrame:frame];
    if (self) {
        UIImpactFeedbackGenerator *feedBackGenertor = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleLight];
        [feedBackGenertor impactOccurred];
        [self loadGroupListFromPlistWithStartY:startY TGroupId:groupId IsManage:manage];
    }
    return self;
}

- (void)loadGroupListFromPlistWithStartY:(CGFloat)startY
                                TGroupId:(NSString *)groupId
                                IsManage:(BOOL)manage
{

    if ([XDXShareInstance shareInstance].ReloadGroupListFlag) {
        [[HomeViewController new] getGroupListSuccess:^(NSArray *data) {
            [self initViewWithRoomArray:data StartY:startY TGroupId:groupId IsManage:manage];
        } Error:^(NSDictionary *error) {
            
        }];
    }else{
        // 获取Documents目录路径
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"GroupList.plist"];
        
        // 检查文件是否存在
        if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
            // 从plist文件读取数据
            NSArray *loadedArray = [NSArray arrayWithContentsOfFile:plistPath];
            if (loadedArray) {
                [self initViewWithRoomArray:loadedArray StartY:startY TGroupId:groupId IsManage:manage];
            }
        }
    }
    
}

- (void)initViewWithRoomArray:(NSArray *)roomArray
                       StartY:(CGFloat)startY
                     TGroupId:(NSString *)groupId
                     IsManage:(BOOL)manage
{
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [self addSubview:bgView];
        
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(16, startY, 0, 0)];
    view.backgroundColor = RealWhiteColor;
    view.layer.masksToBounds = YES;
    [view.layer setCornerRadius:20];
    [self addSubview:view];
    
    UIScrollView *roomView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 180, 0)];
    roomView.showsHorizontalScrollIndicator = NO;
    [view addSubview:roomView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    [[tap rac_gestureSignal] subscribeNext:^(id x) {
        [UIView animateWithDuration:0.3 animations:^{
            view.alpha = 0;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }];
    [bgView addGestureRecognizer:tap];
    
    
    CGFloat Y = 0 ;
    for (int i=0; i<roomArray.count; i ++) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, Y, roomView.width, 42+(i==0?8:0)+(i==(roomArray.count-1)?2:0))];
        [roomView addSubview:button];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(16, (i==0?8:0), button.width-32, 42)];
        label.font = MEDIUM_FONT(15);
        label.text = [NSString stringWithFormat:@"%@",roomArray[i][@"name"]];
        [button addSubview:label];
        
        if ([roomArray[i][@"id"] integerValue]==groupId.integerValue) {
            button.backgroundColor = [DeepMainCoolor colorWithAlphaComponent:0.15];
            label.textColor = NormalMainColor;
                                    
            [label setFrame:CGRectMake(16, (i==0?8:0), button.width-62-([roomArray[i][@"membership"] isKindOfClass:[NSDictionary class]]?33:0), 42)];
            label.numberOfLines = 2;
            [label sizeToFit];
            [button setFrame:CGRectMake(0, Y, roomView.width, label.height+20+(i==0?8:0)+(i==(roomArray.count-1)?2:0))];
            [label setFrame:CGRectMake(16, 10+(i==0?8:0), label.width, label.height)];
            
            UIImageView *roomSel = [[UIImageView alloc] initWithFrame:CGRectMake(button.width-34, (button.height-14)/2, 14, 14)];
            roomSel.contentMode = UIViewContentModeScaleAspectFit;
            roomSel.image = [UIImage imageNamed:@"home_sel"];
            [button addSubview:roomSel];
        }else{
            button.backgroundColor = RealWhiteColor;
            label.textColor = RealBlackColor;
            
            [label setFrame:CGRectMake(16, (i==0?8:0), button.width-32-([roomArray[i][@"membership"] isKindOfClass:[NSDictionary class]]?33:0), 42)];
            label.numberOfLines = 2;
            [label sizeToFit];
            [button setFrame:CGRectMake(0, Y, roomView.width, label.height+20+(i==0?8:0)+(i==(roomArray.count-1)?2:0))];
            [label setFrame:CGRectMake(16, 10+(i==0?8:0), label.width, label.height)];
        }
        if ([roomArray[i][@"membership"] isKindOfClass:[NSDictionary class]]) {
            UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(label.maxX+3, label.y+(label.height-22)/2, 22, 22)];
            if ([roomArray[i][@"membership"][@"planType"] integerValue]==3) {
                img.image = [UIImage imageNamed:@"icon_gold"];
            }else if ([roomArray[i][@"membership"][@"planType"] integerValue]==2){
                img.image = [UIImage imageNamed:@"icon_silver"];
            }else if ([roomArray[i][@"membership"][@"planType"] integerValue]==1){
                img.image = [UIImage imageNamed:@"icon_close"];
            }
            [button addSubview:img];
        }
         
        [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            if (self.RoomManageClickBlock) {
                self.RoomManageClickBlock(roomArray[i]);
            }
            [UIView animateWithDuration:0.3 animations:^{
                view.alpha = 0;
            } completion:^(BOOL finished) {
                [self removeFromSuperview];
            }];
        }];
        Y = CGRectGetMaxY(button.frame)+(i<(roomArray.count-1)?2:0);
        if (i==(roomArray.count-1)) {
            if (Y<(SCREEN_HEIGHT-startY-90)) {
                [roomView setFrame:CGRectMake(0, 0, 180, Y)];
            }else{
                [roomView setFrame:CGRectMake(0, 0, 180, (SCREEN_HEIGHT-startY-90))];
                [roomView setContentSize:CGSizeMake(0, Y)];
                
            }
            if (manage) {
                UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(roomView.frame), roomView.width, 1)];
                line.image = [UIImage imageNamed:@"line"];
                [view addSubview:line];
                
                UIButton *manage = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(line.frame), roomView.width, 42)];
                [view addSubview:manage];
                [[manage rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
                    [UIView animateWithDuration:0.3 animations:^{
                        view.alpha = 0;
                    } completion:^(BOOL finished) {
                        [self removeFromSuperview];
                        if (self.RoomManageBlock) {
                            self.RoomManageBlock();
                        }
                    }];
                }];
                
                UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, manage.width-32, manage.height)];
                label.font = MEDIUM_FONT(15);
                label.text = @"家庭管理";
                label.textColor = RealBlackColor;
                [manage addSubview:label];
                
                
                UIImageView *roomSel = [[UIImageView alloc] initWithFrame:CGRectMake(manage.width-38, (manage.height-18)/2, 18, 18)];
                roomSel.image = [UIImage imageNamed:@"manage"];
                [manage addSubview:roomSel];
                
               
                [UIView animateWithDuration:0.3 animations:^{
                    self.backgroundColor = [RealBlackColor colorWithAlphaComponent:0.4];
                    [view setFrame:CGRectMake(16, startY, 180, CGRectGetMaxY(manage.frame)+8)];
                }];
            }else{
                [UIView animateWithDuration:0.3 animations:^{
                    self.backgroundColor = [RealBlackColor colorWithAlphaComponent:0.4];
                    [view setFrame:CGRectMake(16, startY, 180, roomView.height)];
                }];
            }
        }
    }       
}

@end
